//
//  DateUtils.m
//  e-nurse
//
//  Created by  on 12/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DateUtils.h"
#import "Utils.h"

@protocol DateUtils

+(void)setHourMinOfDate:(NSDate*)date hour:(NSUInteger)hour min:(NSUInteger)min;
+(NSDate*)getLastDayOfMonth:(NSUInteger)monthCount firstDayOfCurrentMonth:(NSDate*)firstDate;

@end

@implementation DateUtils

+(NSDateFormatter*)getDateFormatter:(NSString*)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    
    return [formatter autorelease];  
}

+(NSDateFormatter*)getDefaultDateFormatter
{
    return [DateUtils getDateFormatter:@DATE_FORMAT];
}

+(NSDateFormatter *)getDetailDateFormatter
{ 
    return [DateUtils getDateFormatter:@DATE_DETAIL_FORMAT];
}

+(NSString*)formatDate:(NSDate*)date
{
    NSDateFormatter *formatter = [DateUtils getDefaultDateFormatter];
    return [formatter stringFromDate:date];
}

+(NSDate*)dateFromString:(NSString*)dateStr
{
    NSDateFormatter *formatter = [DateUtils getDefaultDateFormatter];
    return  [formatter dateFromString:dateStr];
}

+(NSDate*)getDateByOffset:(NSUInteger)dayOffset currentDate:(NSDate*)currentDate
{
    NSDateComponents* dateComp = [[NSDateComponents alloc] init];
    [dateComp setDay:dayOffset];
    NSDate* result = [[NSCalendar currentCalendar] dateByAddingComponents:dateComp toDate:currentDate options:0];
    [dateComp release];
    
    return result;
}

+(NSDateComponents *)setHourMinOfDate:(NSDate*)date hour:(NSUInteger)hour min:(NSUInteger)min
{
    NSDateComponents *dayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
    [dayComponents setHour:hour];
    [dayComponents setMinute:min]; 
    
    return dayComponents;
}

+(void)getStartEndDay:(NSDate**)startDay endDay:(NSDate**)endDay startDate:(NSDate*)startDate endDate:(NSDate*)endDate
{
    NSDateComponents *startDayComponents =[DateUtils setHourMinOfDate:startDate hour:0 min:0];
    *startDay = [[NSCalendar currentCalendar] dateFromComponents:startDayComponents];
    
    NSDateComponents *endDayComponents = [DateUtils setHourMinOfDate:endDate hour:23 min:59];
    *endDay = [[NSCalendar currentCalendar] dateFromComponents:endDayComponents];
}

+(void)getWeeksRange:(NSDate**)startDay endDay:(NSDate**)endDay currentDay:(NSDate*)currentDate weekCount:(NSInteger)count
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:currentDate];
    NSInteger weekDay = [components weekday];
    NSDate* startDate = [DateUtils getDateByOffset:(1 - weekDay) currentDate:currentDate];
    NSDate* endDate = [DateUtils getDateByOffset:(7 - weekDay + 7 * (count - 1)) currentDate:currentDate];
    
    [DateUtils getStartEndDay:startDay endDay:endDay startDate:startDate endDate:endDate];
    
//    NSLog(@"%@ : %@\n: %@ : %@", [Utils formatDate:*startDay], [Utils formatDate:*endDay], [Utils formatDate:startDate], [Utils formatDate:endDate]);
}

+(NSDate*)getLastDayOfMonth:(NSUInteger)monthCount firstDayOfCurrentMonth:(NSDate*)firstDate
{
    NSDateComponents *monthComp = [[NSDateComponents alloc] init];
    [monthComp setMonth:(monthCount - 1)];
    NSDate* lastDate = [[NSCalendar currentCalendar] dateByAddingComponents:monthComp toDate:(firstDate) options:0];
    [monthComp release];
    
    NSRange monthRange = [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:lastDate];
    lastDate = [DateUtils getDateByOffset:(monthRange.length - 1) currentDate:lastDate];
    
    NSDateComponents* endDayComp = [DateUtils setHourMinOfDate:lastDate hour:23 min:59];
    return [[NSCalendar currentCalendar] dateFromComponents:endDayComp];  
}

+(void)getMonthsRange:(NSDate**)startDay endDay:(NSDate**)endDay currentDay:(NSDate*)currentDate monthCount:(NSInteger)count
{    
    NSDateComponents *dayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate];
    [dayComponents setDay:1];
    [dayComponents setHour:0];
    [dayComponents setMinute:0];
    *startDay = [[NSCalendar currentCalendar] dateFromComponents:dayComponents];
    
    *endDay = [DateUtils getLastDayOfMonth:count firstDayOfCurrentMonth:(*startDay)];
    
//    NSLog(@"%@ : %@\n", [Utils formatDate:*startDay], [Utils formatDate:*endDay]);
}

+(void)getCurrentSeasonRange:(NSDate**)startDay endDay:(NSDate**)endDay currentDay:(NSDate*)currentDate
{
    NSDateComponents *dayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate];
    [dayComponents setDay:1];
    [dayComponents setHour:0];
    [dayComponents setMinute:0];
    
    if(dayComponents.month <= 3)//Season 1
    {
        [dayComponents setMonth:1];
    }
    else if(dayComponents.month <= 6)//Season 2
    {
        [dayComponents setMonth:4];
    }
    else if(dayComponents.month <= 9)//Season 3
    {
        [dayComponents setMonth:7];
    }
    else //Season 4
    {
        [dayComponents setMonth:10];
    }
    *startDay = [[NSCalendar currentCalendar] dateFromComponents:dayComponents];
    *endDay = [DateUtils getLastDayOfMonth:3 firstDayOfCurrentMonth:*startDay];
    
//    NSLog(@"%@ : %@\n", [Utils formatDate:*startDay], [Utils formatDate:*endDay]);
}

+(BOOL)isDayOfDate:(NSDate*)date
{
    NSDateComponents *hourComponents = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
    return [hourComponents hour] >= 6 && [hourComponents hour] < 19;
    
}

+(BOOL)isNightOfDate:(NSDate*)date
{
    NSDateComponents *hourComponents = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
    return [hourComponents hour] >= 19 || [hourComponents hour] < 6;
}
@end
